home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / +system+ / tools / sound / ahi / developer / devloper.lzx / examples / Device / RecordTest / RecordTest.c < prev    next >
C/C++ Source or Header  |  1979-10-21  |  3KB  |  114 lines

  1. /*
  2. ** This program uses the device interface to sample sound data.
  3. ** The output is written to THE DEFAULT OUTPUT, make sure you
  4. ** start it with "RecordTest > mysample.raw" !
  5. */
  6.  
  7. #include <devices/ahi.h>
  8. #include <dos/dosasl.h>
  9. #include <proto/exec.h>
  10. #include <proto/dos.h>
  11. #include <proto/ahi.h>
  12. #include <stdlib.h>
  13.  
  14. #define FREQUENCY 8000
  15. #define TYPE       AHIST_M8S
  16. #define BUFFERSIZE 20000
  17.  
  18. struct Library    *AHIBase;
  19. struct MsgPort    *AHImp=NULL;
  20. struct AHIRequest *AHIio=NULL;
  21. BYTE               AHIDevice=-1;
  22.  
  23. __far BYTE buffer1[BUFFERSIZE];
  24. __far BYTE buffer2[BUFFERSIZE];
  25.  
  26. void cleanup(LONG rc)
  27. {
  28.   if(!AHIDevice)
  29.     CloseDevice((struct IORequest *)AHIio);
  30.   DeleteIORequest((struct IORequest *)AHIio);
  31.   DeleteMsgPort(AHImp);
  32.   exit(rc);
  33. }
  34.  
  35. void main(int argc, char *argv[])
  36. {
  37.   BYTE *p1=buffer1,*p2=buffer2,*tmp;
  38.   ULONG signals;
  39.  
  40.   if(AHImp=CreateMsgPort()) {
  41.     if(AHIio=(struct AHIRequest *)CreateIORequest(AHImp,sizeof(struct AHIRequest))) {
  42.       AHIio->ahir_Version = 4;
  43.       AHIDevice=OpenDevice(AHINAME,0,(struct IORequest *)AHIio,NULL);
  44.     }
  45.   }
  46.  
  47.   if(AHIDevice) {
  48.     Printf("Unable to open %s/0 version 4\n",AHINAME);
  49.     cleanup(RETURN_FAIL);
  50.   }
  51.  
  52. // Initialize the first read
  53.   AHIio->ahir_Std.io_Command=CMD_READ;
  54.   AHIio->ahir_Std.io_Data=&buffer1;
  55.   AHIio->ahir_Std.io_Length=BUFFERSIZE;
  56.   AHIio->ahir_Std.io_Offset=0;
  57.   AHIio->ahir_Frequency=FREQUENCY;
  58.   AHIio->ahir_Type=TYPE;
  59.   if(!DoIO((struct IORequest *) AHIio)) {
  60.  
  61. // The first buffer is now filled
  62.     SetIoErr(NULL);
  63.  
  64.     for(;;) {
  65.       ULONG length;
  66.       
  67.       length=AHIio->ahir_Std.io_Actual;
  68.  
  69. // Initialize the second read (note that io_Offset is not cleared!)
  70.       AHIio->ahir_Std.io_Data=p2;
  71.       AHIio->ahir_Std.io_Length=BUFFERSIZE;
  72.       AHIio->ahir_Frequency=FREQUENCY;
  73.       AHIio->ahir_Type=TYPE;
  74.       SendIO((struct IORequest *) AHIio);
  75.  
  76. // While the second read is in progress, save the first buffer to stdout
  77.       if(Write(Output(),p1,length) != length) {
  78.         break;
  79.       }
  80.  
  81.       signals=Wait(SIGBREAKF_CTRL_C | (1L << AHImp->mp_SigBit));
  82.  
  83.       if(signals & SIGBREAKF_CTRL_C) {
  84.         SetIoErr(ERROR_BREAK);
  85.         break;
  86.       }
  87.  
  88. // Remove the reply 
  89.       if(WaitIO((struct IORequest *) AHIio)) {
  90.         SetIoErr(ERROR_READ_PROTECTED);
  91.         break;
  92.       }
  93.  
  94. // Swap buffer pointers and repeat
  95.       tmp=p1;
  96.       p1=p2;
  97.       p2=tmp;
  98.     }
  99.  
  100. // Abort any pending iorequests
  101.     AbortIO((struct IORequest *) AHIio);
  102.     WaitIO((struct IORequest *) AHIio);
  103.   }
  104.  
  105.   if(IoErr())
  106.   {
  107.     PrintFault(IoErr(), argv[0] ); // Oh, common! It's not MY fault that this
  108.                                    // routine prints to stdout instead of stderr!
  109.     cleanup(RETURN_ERROR);
  110.   }
  111.  
  112.   cleanup(RETURN_OK);
  113. }
  114.